package com.wangsai.framework.supersecurity.satoken;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import com.wangsai.framework.supersecurity.core.config.SuperSecurityConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Mr.Wang
 * @description
 * @date 2021-08-16 11:24
 */
@RequiredArgsConstructor
public class SaWebMvcConfigurer implements WebMvcConfigurer {

    private final SuperSecurityConfig superSecurityConfig;

    /**
     * 注册Sa-Token的注解拦截器，打开注解式鉴权功能
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] excludePathPatterns = {};
        if (superSecurityConfig != null) {
            excludePathPatterns = superSecurityConfig.getExcludePathPatterns();
        }
        // 注册Sa-Token的路由拦截器，并排除登录接口或其他可匿名访问的接口地址 (与注解拦截器无关)
        registry.addInterceptor(new SaRouteInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
        // 注册注解拦截器，并排除不需要注解鉴权的接口地址 (与登录拦截器无关)
        registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
    }
}